package com.haiqiu.tools.test.controller;

import com.aliyun.oss.model.OSSObjectSummary;
import com.haiqiu.file.ftp.utils.FtpUtils;
import com.haiqiu.file.oss.utils.OssUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * oss文件上传测试
 *
 * @author haiqiu
 * @date 2022/6/28
 */
@RestController
@RequestMapping("/oss")
public class OssController {

    public static final String PATH = "/test/document";

    /**
     * 单文件上传
     *
     * @param file 文件
     */
    @PostMapping(value = "upload")
    public Object uploads(@RequestParam("file") MultipartFile file) {
        String filePath = OssUtils.uploadFile("/test/document", file);
        if (filePath != null) {
            System.out.println("文件：" + file.getOriginalFilename() + "上传成功,服务器路径：" + filePath);
        }
        return filePath;
    }

    /**
     * 多文件上传
     *
     * @param files 文件批量上传
     */
    @PostMapping(value = "uploads")
    public Object uploads(@RequestPart(value = "files") List<MultipartFile> files) {
        Set<String> filePaths = new HashSet<>();
        for (MultipartFile file : files) {
            String filePath = OssUtils.uploadFile(PATH, file);
            if (filePath != null) {
                System.out.println("文件：" + file.getOriginalFilename() + "上传成功,服务器路径：" + filePath);
                filePaths.add(filePath);
            }
        }
        return filePaths;
    }

    /**
     * 单文件下载
     * 此接口可以预览图片，文档，文本等常见的文件浏览器打开预览
     *
     * @param fileName 文件
     */
    @GetMapping(value = "download/{fileName}")
    public byte[] download(@PathVariable("fileName") String fileName) {
        String remoteFilePath = PATH + "/" + fileName;
        return OssUtils.download(remoteFilePath);
    }

    /**
     * 单文件下载
     * 返回文件二进制流
     *
     * @param fileName 文件
     */
    @GetMapping(value = "downloadFile/{fileName}")
    public void downloadFile(@PathVariable("fileName") String fileName) {
        String remoteFilePath = PATH + "/" + fileName;
        OssUtils.downloadFile(remoteFilePath);
    }


    /**
     * 预览图片
     *
     * @param fileName 文件路径
     * @return 文件流
     * @throws Exception
     */
    @GetMapping(value = "/img/{fileName}", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] getImage(@PathVariable("fileName") String fileName) throws Exception {
        String remoteFilePath = PATH + "/" + fileName;
        byte[] bytes = FtpUtils.download(remoteFilePath);
        assert bytes != null;
        BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(bytes));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        String type = fileName.substring(fileName.lastIndexOf(".") + 1);
        ImageIO.write(bufferedImage, type.toLowerCase(), out);
        return out.toByteArray();
    }


    /**
     * 查看oss集合列表
     *
     * @param path 路径前缀
     * @return 集合信息
     */
    @GetMapping(value = "list")
    public List<OSSObjectSummary> list(@RequestParam(required = false) String path) {
        if (path != null && path.length() > 0) {
            return OssUtils.list(path);
        }
        return OssUtils.list(null);
    }


    /**
     * 删除oss文件
     *
     * @param remotePath 文件地址
     * @return 删除成功
     */
    @DeleteMapping
    public boolean delete(@RequestParam String remotePath) {
        return OssUtils.deleteFile(remotePath);
    }

}
